package com.it.a08test2;

import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.OutputStream;
import java.net.Socket;

public class Client {
    public static void main(String[] args) throws IOException {
        //客户端：发送一条数据，接收服务端反馈的消息并打印
        //服务器：接收数据并打印，再给客户端反馈消息
        //创建Socket对象
        Socket sock = new Socket("127.0.0.1", 8888);

        //输出数据
        String str = "见到你很高兴";
        OutputStream os = sock.getOutputStream();
        os.write(str.getBytes());

        //写一个结束标志
        sock.shutdownOutput();

        //接收服务端的反馈消息
        InputStream is = sock.getInputStream();
        InputStreamReader isr = new InputStreamReader(is);
        int c;
        while ((c = isr.read()) != -1) {
            System.out.print((char) c);
        }

        //释放数据
        sock.close();
    }
}
